﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using FasterWPF;
using FasterWPFDemo.UserControls;
using FasterWPF.Selectors;

namespace FasterWPFDemo
{
    public static class TabItemActions
    {
        public static void RemoveUserControl<T>(string tabControlGuid, string userCtlGuid, string storeName, T tabItemUserControl, UserControlRegion tabItemRegion, string tabItemRegionkey)
               where T: UserControl
        {
            //STORE ACTION: Remove User Control
            GlobalStoreService.PutInGlobalStore(tabControlGuid, storeName, new Action<object, RoutedEventArgs>((o, ev) =>
            {
                MessageBox.Show("I was just clicked: Remove me!");

                //OLD CODE
                //NOTE: RIGHT NOW THIS IS HARDCODED MERELY FOR THIS EXAMPLE - IN A PRODUCTION APP YOU WOULD WANT TO CHANGE THIS!!!
                //Border buttonInsideUserControl = tabItemUserControl.GetChildFromParent<Border, UserControl>(userCtlGuid, 0, 4);
                ////UnHook click event on button - in a Production app you'd likel want to unhook all events on removing a user control!!!      //buttonInsideUserControl.DisposeEvents<Border>();

                //NEW CODE
                tabItemUserControl.DisposeEventsOnAllParents();

                //Remove UserControl from UserControlRegion
                Border userControlChildComposite3a = null;

                if (tabItemRegion.TrySelectCompositeByKey(userCtlGuid, out userControlChildComposite3a))
                {
                    tabItemRegion.RemoveByKey<Border>(tabItemRegionkey);
                }               

                ((Action<object, RoutedEventArgs>)Application.Current.Properties["ResizeCorrectly"])(o, ev);
            }));
        }

        public static string AddUserControl<T>(string tabControlGuid, string userCtlGuid, string storeName, T tabItemUserControl, UserControlRegion tabItemRegion, string tabItemRegionkey)
               where T : UserControl, IUserControlGuid, new()
        {
            //Remove the old references to the prior UserControl to let it be garbage-collected now
            GlobalStoreService.RemoveFromGlobalStore(tabControlGuid, storeName);

            //STORE ACTION: Add User Control
            GlobalStoreService.PutInGlobalStore(tabControlGuid, storeName, new Action<object, RoutedEventArgs>((o, ev) =>
            {
                MessageBox.Show("I was just clicked: Add me!");

                //Remove first, if it already exists
                if (tabItemRegion.GetCompositeLookupDictionary<Border>() != null && tabItemRegion.GetCompositeLookupDictionary<Border>().Count > 0)
                {
                    //OLD CODE
                    ////NOTE: RIGHT NOW THIS IS HARDCODED MERELY FOR THIS EXAMPLE - IN A PRODUCTION APP YOU WOULD WANT TO CHANGE THIS!!!
                    //Border buttonInsideUserControl = tabItemUserControl.GetChildFromParent<Border, UserControl>(userCtlGuid, 0, 4);
                    ////UnHook click event on button - in a Production app you'd likel want to unhook all events on removing a user control!!!          //buttonInsideUserControl.DisposeEvents<Border>();

                    //NEW CODE
                    tabItemUserControl.DisposeEventsOnAllParents();

                    //Remove UserControl from UserControlRegion
                    Border userControlChildComposite3a = null;

                    if (tabItemRegion.TrySelectCompositeByKey(userCtlGuid, out userControlChildComposite3a))
                    {
                        tabItemRegion.RemoveByKey<Border>(tabItemRegionkey);
                    }
                }

                //Add new user control
                T tabItemUserControl2 = new T();
                tabItemUserControl2.UserControlGuid = userCtlGuid;
                tabItemUserControl2.LoadUserControl();
                tabItemRegion.AddUserControl(tabItemUserControl2, tabItemRegionkey);

                ((Action<object, RoutedEventArgs>)Application.Current.Properties["ResizeCorrectly"])(o, ev);
            }));
            return tabItemRegionkey;
        }

    }
}
